#include <QtGui>
#include <QtDebug>
#include <QSettings>
#include <QDockWidget>

#include "ui_maindepartment.h"

#include "maindepartment.h"
#include "source/connect.h"
#include "source/weektable.h"
#include "source/prospectivestudents.h"

#include "source/listteacher.h"
#include "source/listgroup.h"
#include "source/listroom.h"
#include "source/listsubject.h"
#include "source/listspec.h"

MainDepartment::MainDepartment(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainDepartment)
{
    //QTextCodec *utfcodec = QTextCodec::codecForName("UTF-8");
    //QTextCodec::setCodecForTr(utfcodec);

    ui->setupUi(this);
    createToolBars();

    setStyleSheet(QLatin1String(" QToolBar QToolButton {  height: 36px; width: 36px;}"));

    qDebug() << QString("EduHead_" + QLocale::system().name());
}

MainDepartment::~MainDepartment()
{
    delete ui;
}

void MainDepartment::addDock(QWidget *obj, QString nameobj, QString namewin)
{
    QDockWidget *dock = new QDockWidget(namewin);
    dock->setObjectName(nameobj);
    dock->setWidget(obj);
    addDockWidget(Qt::LeftDockWidgetArea, dock);
}

void MainDepartment::createToolBars()
{

    DirectoryToolBar = addToolBar(tr("Directory"));
    DirectoryToolBar->setIconSize(QSize(30,30));

    DirectoryToolBar->addAction(ui->action_mGroup);
    DirectoryToolBar->addAction(ui->action_mTeacher);
    DirectoryToolBar->addAction(ui->action_mSubject);
    DirectoryToolBar->addAction(ui->action_mRoom);

    ScheduleToolBar = addToolBar(tr("RaspGrp"));
    ScheduleToolBar->setIconSize(QSize(30,30));

    ScheduleToolBar->addAction(ui->action_mRaspisGroup);
    ScheduleToolBar->addAction(ui->action_mRaspisTeacher);
    ScheduleToolBar->addAction(ui->action_mRaspisRoom);

    ScheduleToolBar->addSeparator();

    ScheduleToolBar->addAction(ui->action_mShemaGroup);
    ScheduleToolBar->addAction(ui->action_mShemaTeacher);
    ScheduleToolBar->addAction(ui->action_mShemaRoom);

}

void MainDepartment::on_action_mGroup_triggered()
{
    ListGroup *child = new ListGroup(this);
    ui->mdiArea->addSubWindow(child);
    child->show();
}

void MainDepartment::on_action_mTeacher_triggered()
{
    ListTeacher *child = new ListTeacher();
    //ui->mdiArea->addSubWindow(child);
    //child->show();
    addDock(child, tr("TeacherDock"), tr("Teacher"));
}

void MainDepartment::on_action_mSubject_triggered()
{
    ListSubject *child = new ListSubject(this);
    ui->mdiArea->addSubWindow(child);
    child->show();
}

void MainDepartment::on_action_mRoom_triggered()
{
    ListRoom *child = new ListRoom(this);
    ui->mdiArea->addSubWindow(child);
    child->show();
}

//
void MainDepartment::on_action_mExit_triggered()
{
    close();
}

void MainDepartment::on_action_mConnectBase_triggered()
{
    Connect *uc = new Connect(this);

    connect(uc, SIGNAL(on_Connect()), this, SIGNAL(UpdateDB()));

    uc->exec();
}

void MainDepartment::on_action_mRaspisGroup_triggered()
{
    WeekTable *child = createRaspisGroup();
     //child->newFile();
    child->show();
     //addDock(child, tr("RaspGroupDock"), tr("Schedule group"));
}

WeekTable *MainDepartment::createRaspisGroup()
{   
    WeekTable *child = new WeekTable();
    ui->mdiArea->addSubWindow(child);
    child->typeList = WeekTableGroup;
    child->UpdateList();

    return child;
}

void MainDepartment::on_action_mRaspisTeacher_triggered()
{
    WeekTable *child = createRaspisTeacher();
    child->show();
}

WeekTable *MainDepartment::createRaspisTeacher()
{
    WeekTable *child = new WeekTable();
    ui->mdiArea->addSubWindow(child);
    child->typeList = WeekTableMen;
    child->UpdateList();

    return child;
}

void MainDepartment::on_action_mRaspisRoom_triggered()
{

}

void MainDepartment::on_action_mShemaGroup_triggered()
{
    //ShemaGroup *child = new ShemaGroup();
    //ui->mdiArea->addSubWindow(child);
    //child->show();
}

void MainDepartment::on_action_mShemaTeacher_triggered()
{
    //ShemaTeacher *child = new ShemaTeacher();
    //ui->mdiArea->addSubWindow(child);
    //child->show();
}

void MainDepartment::on_action_mShemaRoom_triggered()
{

}

void MainDepartment::on_action_mScheduleGroupDock_triggered()
{
    WeekTable *child = new WeekTable();
    child->typeList = WeekTableGroup;
    child->UpdateList();

    connect(this, SIGNAL(UpdateDB()), child, SLOT(on_TablesUpdate()));

    addDock(child, "ScheduleGroupDock", tr("Schedule group"));
}

void MainDepartment::on_action_mScheduleTeacherDock_triggered()
{
    WeekTable *child = new WeekTable();
    child->typeList = WeekTableTeacher;
    child->UpdateList();

    addDock(child, "ScheduleTeacherDock", tr("Schedule group"));
}

void MainDepartment::on_action_mScheduleRoomDock_triggered()
{
    WeekTable *child = new WeekTable();
    child->typeList = WeekTableRoom;
    child->UpdateList();

    addDock(child, "ScheduleRoomDock", tr("Schedule group"));
}

void MainDepartment::on_action_mSpeciality_triggered()
{
    ListSpec *child = new ListSpec (this);
    ui->mdiArea->addSubWindow(child);
    child->show();
}

void MainDepartment::on_action_mProspective_triggered()
{
    ProspectiveStudents *child = new ProspectiveStudents (this);
    ui->mdiArea->addSubWindow(child);
    child->show();
}
